﻿' LoginPageViewModel.vb
Imports System.Net.Http
Imports System.Windows
Imports CommunityToolkit.Mvvm.ComponentModel
Imports CommunityToolkit.Mvvm.Input
Imports Newtonsoft.Json

Public Class LoginPageViewModel : Inherits ObservableObject
    ' 绑定属性
    Private _userLogin As User
    Public Property UserLogin As User
        Get
            Return _userLogin
        End Get
        Set(value As User)
            SetProperty(_userLogin, value)
        End Set
    End Property

    Private _errorMessage As String
    Public Property ErrorMessage As String
        Get
            Return _errorMessage
        End Get
        Set(value As String)
            SetProperty(_errorMessage, value)
        End Set
    End Property

    Private ReadOnly _userLoginHttpClient As HttpClientBase
    Private _userInfoReaderHttpClient As HttpClientBase

    'Public Sub New()
    '    LoginCommand = New AsyncRelayCommand(AddressOf LoginAsync)
    'End Sub

    Public Sub New()
        _userLoginHttpClient = New HttpClientBase
        _userInfoReaderHttpClient = Nothing
        UserLogin = New User With {
           .Id = Nothing,
           .Password = String.Empty
        }
        LoginCommand = New AsyncRelayCommand(AddressOf LoginAsync)
        ToRegisterPageCommand = New RelayCommand(AddressOf ToRegisterPageClicked)
    End Sub

    Public ReadOnly Property LoginCommand As IAsyncRelayCommand
    Public ReadOnly Property ToRegisterPageCommand As IRelayCommand

    ' 核心登录逻辑
    Private Async Function LoginAsync() As Task
        '模拟登录
        'ErrorMessage = String.Empty
        ''Console.WriteLine(UserLogin.ToString())
        'Console.WriteLine(JsonConvert.SerializeObject(_userLogin))
        'If UserLogin.Id = "2" And UserLogin.Password = "123456" Then
        '    NavigationService.Navigate(MAIN_LAYOUT_URI)
        '    Console.WriteLine("模拟登录成功")
        '    Call SaveUserIdToResources()
        '    Return
        'End If

        Try
            Dim res1 = Await _userLoginHttpClient.PostAsync(Of String)(UserLogin, "vdm/api/account/login")
            'If response.Code = 200 Then
            '    'Dim result = Await response.Content.ReadFromJsonAsync(Of LoginResponse)()
            '    UserLogin.Username = response.Data.Username
            '    Call SaveUserIdToResources()
            '    NavigationService.Navigate(MAIN_LAYOUT_URI)
            'Else
            '    Dim errorContent = response.Message.ToString
            '    ErrorMessage = $"登录失败：{errorContent}"
            'End If
            If res1.Code <> 200 Then
                ErrorMessage = res1.Message.ToString
                Return
            End If
            Call SaveTokenToResources(res1.Data)
            _userInfoReaderHttpClient = New HttpClientBase()
            Dim res2 = Await _userInfoReaderHttpClient.GetAsync(Of User)(String.Format("vdm/api/account/findUser/id/{0}", UserLogin.Id))
            If res2.Code <> 200 Then
                ErrorMessage = res2.Message.ToString
                Return
            End If
            UserLogin = res2.Data
            Call SaveUserIdToResources()
            NavigationService.Navigate(MAIN_LAYOUT_URI)
        Catch ex As HttpRequestException
            ErrorMessage = "服务器连接异常，请检查网络"
        Catch ex As Exception
            ErrorMessage = $"系统错误：{ex.Message}"
        End Try
    End Function

    Private Sub ToRegisterPageClicked()
        NavigationService.Navigate(SIGN_UP_PAGE_URI)
    End Sub

    Private Sub SaveUserIdToResources()
        ' 存储到本地或安全存储
        Application.Current.Resources("UserId") = UserLogin.Id
        Application.Current.Resources("UserName") = UserLogin.Username
    End Sub

    Private Sub SaveTokenToResources(token As String)
        ' 存储到本地或安全存储
        Application.Current.Resources("Token") = token
    End Sub

    ' 辅助方法
    'Private Sub SaveAuthToken(token As String)
    '    ' 存储到本地或安全存储
    '    Application.Current.Resources("AuthToken") = token
    'End Sub

    ' 响应模型
    'Private Class LoginResponse
    '    Public Property Token As String
    'End Class
End Class
